﻿using ResourceSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using UnityEditor;
using UnityEngine;
using System.Reflection;

public class ResUtils
{

    static string resConfigPath = Application.dataPath + "/Resources/Config/Resources.xlsx";

   public static string[] NotBuildFileExtensionFilter = new string[] { ExtensionFilter.Meta, ExtensionFilter.Xlsx , ExtensionFilter.Space };

    public static string[] NotBuildFileDirectoryFilter = new string[] { "Editor" };
    /// <summary>
    /// 初始化信息数据
    /// </summary>
    /// <param name="resPath"></param>
    /// <param name="isWriteExcel"></param>
    public static void InitResInfo(string resPath, bool isWriteExcel = false)
    {

        List<ResourcesData> infos = new List<ResourcesData>();
        SerializeResInfo(resPath, ref infos);

        ResourceSystemFacade.Inst.OnSystemInitFinish += () =>
        {

            foreach (var item in infos)
            {
                DebugUtils.LogRedInfo(item.Name);
                for (int i = 0; i < item.Dependencys.Count; i++)
                {
                    if (string.IsNullOrEmpty(item.Dependencys[i]))
                        continue;
                    for (int j = 0; j < infos.Count; j++)
                    {
                        if (item.Dependencys[i]==infos[j].Name)
                        {
                            infos[j].DependeciedNum++;
                        }
                    }
                }
            }
            if (isWriteExcel)
            {
                string[,] content = ConvertUtils.ConvertInfosToContent(infos);
                ResourceSystemFacade.Inst.WriteToExcel(resConfigPath, "Sheet1", content);
                FileInfo fileInfo = new FileInfo(resConfigPath);
    
                ConfigUtils.GenerateConfigToScriptableObject(ConfigFileType.Excel, fileInfo, ResourceSystemFacade.ScriptableObjectResPath);
            }
            else
            {
                ConfigUtils.CreateScriptableObject<ResourcesInfo>("ResourcesInfo", ConvertUtils.ConvertImplementListToInterfaceList(infos), ResourceSystemFacade.ScriptableObjectResPath);
            }


        };

        ResourceSystemFacade.Inst.InitSystem(false);

    }

    /// <summary>
    /// 生成需要更新的资源信息
    /// </summary>
    public static void GenerateUpgradeResourcesInfo()
    {
        //todo
        //获取当前旧版的资源的数据
        //对比新版的资源数据
        //生成新版的更新信息
    }

    public static Dictionary<string,ResourcesData> GetPreviousVersionResData()
    {
        Dictionary<string, ResourcesData> infoDict = new Dictionary<string, ResourcesData>();
        string previousVersion = ResourceSystemFacade.Inst.GetPreviousVersion();
        string fullPath = ResourceSystemFacade.ResInfoConfigPath + previousVersion+".asset";
        ResourcesInfo resInfo=  AssetDatabase.LoadAssetAtPath<ResourcesInfo>(PathUtils.GetRelativePath("Assets",fullPath));
        if (resInfo!=null)
        {
            for (int i = 0; i < resInfo.Data.Count; i++)
            {
                infoDict.Add(resInfo.Data[i].Name, resInfo.Data[i]);
            }
        }else
        {
            DebugUtils.LogRedInfo("没有该文件" + fullPath);
        }
        return infoDict;
    }

    /// <summary>
    /// 序列化资源数据
    /// </summary>
    /// <param name="resPath"></param>
    /// <param name="isWriteExcel"></param>
    static void SerializeResInfo(string resPath,ref List<ResourcesData> infos)
    {
        string[] fileNames = Directory.GetFiles(resPath);
        string[] dirNames = Directory.GetDirectories(resPath);
        for (int i = 0; i < dirNames.Length; i++)
        {
            SerializeResInfo(dirNames[i], ref infos);
        }

        List<string > tempfiles=ResFileUtils.SelectFilesByFileFilter(fileNames, NotBuildFileExtensionFilter);
        List<string> files = ResFileUtils.SelectFilesExceptDirectoryPath(tempfiles.ToArray(), NotBuildFileDirectoryFilter);
        for (int i = 0; i < files.Count; i++)
        {
            ResourcesData info = new ResourcesData();
            info.ID = ResFileUtils.GetFileMD5(files[i]);
            info.Name = PathUtils.GetFileNameWithoutExtension(files[i]);
            info.Path = PathUtils.GetRelativePathWithoutRootAndFileName("Resources", files[i]);
            info.IsEmpty = false;
            Debug.Log(info.Path + info.Name);
            string assetPath = PathUtils.GetRelativePath("Assets", files[i]);
            string[] dependencyPath = AssetDatabase.GetDependencies(assetPath);
            for (int j = 0; j < dependencyPath.Length; j++)
            {
                if (assetPath != dependencyPath[j])
                    info.Dependencys.Add(PathUtils.GetFileNameWithoutExtension(dependencyPath[j]));
            }
            info.version = "";
            infos.Add(info);
        }
    }

}